﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using Ext.Net;
using Ext.Net.Utilities;

namespace KamLib.Controls
{

    public class DshItem
    {
        public string ID { get; set; }
        public int TabGroup { get; set; }
        public int TabIndex { get; set; }
        public string Pager { get; set; }
        public int PageIndex { get; set; }
        public string Icon { get; set; }
        public string Title { get; set; }
        public string Desc { get; set; }
        public string Click { get; set; }
        public string QTip { get; set; }
    }

    public class DshGroup
    {
        public const string DefaultIcon = "resources/images/icon.png";

        public int TabGroup { get; set; }
        public string Title { get; set; }
        public string BgColor { get; set; }
        public string BorderColor { get; set; }
        public string Pager { get; set; }

        private DshItem[] _Items;
        public DshItem[] Items
        {
            get { return _Items; }
            set
            {
                _Items = value;
                foreach (var item in _Items)
                {
                    if (item.TabGroup == 0)
                        item.TabGroup = TabGroup;
                    if (item.Pager.IsEmpty())
                        item.Pager = Pager;
                    if (item.Icon.IsEmpty())
                        item.Icon = DefaultIcon;
                }
            }
        }
    }

    public enum DashboardButtonKind { Simple, Detail };

    public class Dashboard : Control
    {
        int _groupColumnCount = 1;
        public int GroupColumnCount { get { return _groupColumnCount; } set { _groupColumnCount = value; } }

        int _columnCount = 6;
        public int ColumnCount { get { return _columnCount; } set { _columnCount = value; } }

        public DashboardButtonKind ButtonKind { get; set; }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            DV.Template.Html = DV.Template.Html.
                Replace("{GroupColumnCount}", GroupColumnCount.ToString()).
                Replace("{ColumnCount}", ColumnCount.ToString());
        }

        public object DataSource
        {
            set
            {
                CreateChildControls();
                Store.DataSource = PrepareData(value);
                Store.DataBind();
            }
        }

        private object PrepareData(object value)
        {
            var pages = value as IEnumerable<PageNode>;
            var page = value as PageNode;
            if (pages == null && page == null) return value;

            if (page != null) pages = new[] { page };

            bool useDesc = ButtonKind == DashboardButtonKind.Detail;

            var groups = new List<DshGroup>();
            foreach (var p in pages)
            {
                groups.Add(new DshGroup
                {
                    TabGroup = p.Index + 1,
                    Title = p.Title,
                    BgColor = p.BgColor,
                    BorderColor = p.BorderColor,
                    Items = p.Items.Where(a => a.Icon != null).Select(a => new DshItem
                    {
                        ID = a.LID,
                        Icon = a.Icon,
                        Title = a.Title,
                        Desc = useDesc ? a.Desc ?? "" : "",
                        TabGroup = p.Index + 1,
                        TabIndex = a.Index + 1,
                        Click = a.Click,
                        QTip = HttpUtility.HtmlEncode(a.Desc),
                    }).ToArray(),
                });
            }

            return groups;
        }

        protected Store Store;
        protected DataView DV;

        protected override void CreateChildControls()
        {
            if (ChildControlsCreated) return;
            ChildControlsCreated = true;


            DV = new DataView
            {
                ID = ID + "_DV",
                Store = 
                {
                    (Store = new Store
                    {
                        ID = ID + "_Store",
                        Reader =
                        {
                            new JsonReader
                            {
                                Fields = 
                                {
                                    new RecordField { Name = "TabGroup" },
                                    new RecordField { Name = "Title" },
                                    new RecordField { Name = "BgColor" },
                                    new RecordField { Name = "BgColor" },
                                    new RecordField { Name = "BorderColor" },
                                    new RecordField { Name = "Items", IsComplex = true },
                                }
                            }
                        }
                    })
                },
                SingleSelect = true,
                OverClass = "x-btn-over",
                ItemSelector = "table.dsh-item",
                AutoHeight = true,
                EmptyText = "No items to display",
                Template =
                {
                    Html =
@"<div class=""x-unselectable x-toolbar x-inline-toolbar""><div class=""dsh-items-ct"">
<tpl for=""."">
    <div class=""dsh-group-header"" style=""background-color:{BgColor};border-color:{BorderColor};"">
    <h2 id=""GroupPage:{TabGroup}"">{Title}</h2>
    <dl>
    <tpl for=""Items"">
<table id=""{ID}:{TabGroup}:{TabIndex}:{Pager}:{PageIndex}"" cellspacing=""0"" class=""x-btn dsh-item"" onclick=""{Click}"">
<tbody>
<tr><td class=""x-btn-tl""><i>&nbsp;</i></td><td class=""x-btn-tc""></td><td class=""x-btn-tr""><i>&nbsp;</i></td></tr>
<tr>
    <td class=""x-btn-ml""><i>&nbsp;</i></td>
    <td class=""x-btn-mc"" valign=""top""><div class=""dsh-item-wrap""><div class=""dsh-item-icon icon-{Icon}-64s""></div><h6>{Title}</h6><div class=""dsh-item-desc"">{Desc}</div></div></td>
    <td class=""x-btn-mr""><i>&nbsp;</i></td>
</tr>
<tr><td class=""x-btn-bl""><i>&nbsp;</i></td><td class=""x-btn-bc""></td><td class=""x-btn-br""><i>&nbsp;</i></td></tr>
</tbody>
</table>
<tpl if=""xindex % {ColumnCount} == 0""><div class=""x-clear""></div></tpl>
    </tpl>
    </dl>
    <div class=""x-clear""></div>
    </div>
    <tpl if=""xindex % {GroupColumnCount} == 0""><div class=""x-clear""></div></tpl>
</tpl>
</div></div>"
                },
                Listeners =
                {
                    SelectionChange = { Fn = "KamLib.Dashboard1.selectionChanged" },
                    ContainerClick = { Fn = "KamLib.Dashboard1.viewClick" }
                }
            };

            Controls.Add(DV);
        }
    }
}
